var sliderWidth = 106; // 需要设置slider的宽度，用于计算中间位置
var week = new Date().getDay(); // 获取当前时间,用于显示当前课表
var app = getApp();
var openId;
Page({
    data: {
        activeIndex: "0",
        sliderOffset: 0,
        sliderLeft: 0,
        FPOwinWidth:0,
        subject:"",
        Mon:'',
        Tus:'',
        wed:'',
        Thus:'',
        Fir:'',
        loading: true
    },

    onLoad: function () {
        var _this = this
        app.getUserInfo(function(res){
            //console.log(res); // openid
            openId = res;
            //console.log('onLoad执行')
            //判断是否绑定学号密码
            wx.getStorage({
                key: 'isRegist',
                fail: function(){
                    wx.request({
                        // url: 'http://246.ameiity.sinaapp.com/wxapp/php/check.php?openid='+openId,
                        url: 'https://ameiity.applinzi.com/wxapp/php/check.php?openid='+openId,
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function (res) {
                            wx.hideToast();
                            //console.log(res);
                            if(res.data)
                                if(!res.data.regist){
                                    //console.log('送你去绑定')
                                    wx.redirectTo({
                                        url: '../regist/regist'
                                    })
                                }else{
                                    _this.requestApi()
                                }
                        },
                    });
                },
                success: function(){
                    //console.log('get isRegist 成功')
                    wx.getStorage({
                        key: 'kebiao',
                        success: function(kebiao){
                            //console.log('get kebiao 成功')
                            var subject = kebiao.data
                            //console.log(subject)
                            _this.setData({
                                subject: subject,
                                Mon: subject[0].class,
                                Tus: subject[1].class,
                                wed: subject[2].class,
                                Thus: subject[3].class,
                                Fir: subject[4].class,
                                loading: false
                            })
                        },
                        fail: function(){
                            //console.log('get kebiao 失败')
                            _this.requestApi()
                        },
                    })
                }
            })
        });

        // 获取系统信息
        wx.getSystemInfo({
            success: function(res) {
                var w = week;
                if ( w == 6 || w == 0) {
                    w = 1; // 周末则默认显示星期一的课表
                }
                _this.setData({
                    FPOwinWidth: res.windowWidth / 5, // 窗口宽度 / 5
                    sliderOffset: (res.windowWidth / 5) * ( w - 1),
                    activeIndex: w - 1,
                    sliderLeft: (res.windowWidth / 5 - sliderWidth) / 2
                });
            }
        });
    },

    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },

    // 请求课表的api
    requestApi(){
        console.log('进入api')
        var _this = this
        wx.request({
            url: 'https://ameiity.applinzi.com/wxapp/api/api_kebiao.php?openid='+openId, 
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                if(res.data == 0){
                    //console.log('账号或密码错误');
                    wx.showModal({
                        content: '学号或密码错误请重新绑定学号密码',
                        showCancel: false,
                        success: function (res) {
                            if(res.confirm){
                                //console.log('用户点击确定')
                                wx.redirectTo({
                                    url: '../regist/regist'
                                })
                            }
                        }
                    });
                }else{
                    if(res.data == 1){
                        console.log('系统繁忙')
                        wx.showModal({
                            title: '教务系统崩溃了',
                            content: '系统繁忙,读取备份数据中...',
                            showCancel: false,
                            success: function (res) {
                                if(res.confirm){
                                    //console.log('用户点击确定，返回主界面')
                                    // wx.navigateBack({
                                    //     delta: 1
                                    // })
                                    wx.getStorage({
                                        key: 'kebiao_bak',
                                        success: function(kebiao_bak){
                                            wx.setData({
                                                key: 'kebiao',
                                                data: kebiao_bak
                                            })
                                            wx.redirectTo({
                                                url: './kebiao'
                                            })
                                        },
                                        fail: function(){
                                            wx.showModal({
                                                content: '无备份数据,请等待教务系统开放,带来不便,sorry',
                                                showCancel: false,
                                                success: function(res){
                                                    if(res.confirm){
                                                        wx.navigateBack({
                                                            delta: 1
                                                        })
                                                    }
                                                }
                                            })
                                        }
                                    })
                                }
                            }
                        });
                    }else{
                        if(res.data == 2){
                            wx.showModal({
                                content: '课表更新中，暂时无法查询',
                                showCancel: false,
                                success: function (res) {
                                    if (res.confirm) {
                                        //console.log('用户点击确定，返回主界面')
                                        wx.navigateBack({
                                            delta: 1
                                        })
                                    }
                                }
                            });
                        }else{
                            var subject = res.data // 用临时变量存储一份,防止下面过度访问res.data
                            _this.setData({
                                subject: subject,
                                Mon: subject[0].class,
                                Tus: subject[1].class,
                                wed: subject[2].class,
                                Thus: subject[3].class,
                                Fir: subject[4].class,
                                loading: false
                            })

                            // 将课表信息 写入缓存
                            wx.setStorage({
                                key: 'kebiao',
                                data: subject
                            })
                            wx.setStorage({
                                key: 'kebiao_bak',
                                data: subject
                            })
                            wx.setStorage({
                                key: 'isRegist',
                                data: true // 因为请求到了这 表示一定绑定了
                            })
                        }
                    }
                }
            }
        })
    }
});